package com.company.zonghelizi.xml;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 1.编写dtd文件
 * 2.编写schema
 * 3.解析该文件，要求包里面的相关数据封装到Student.class里
 * 放到ArrayList里
 * 4.打印出所有女生的平均年龄
 * 5. 对年龄排序
 * 6. 排好序的数据输出该xml里面
 * 7. 把年龄最大的这个学生的性别换掉
 * 9. 最后再把这个人干掉
 */
public class Test {
    static List<Student> list = new ArrayList<>();
    static Student student = new Student();
    public static void main(String[] args) throws DocumentException, IOException {
        int count = 0;
        // 不变的访问文件
        File xmlFile = new File("pkg/xml/student.xml");
        SAXReader reader = new SAXReader();
        Document xmlDoc = reader.read(xmlFile);

        // 读放到list中
        Element root = xmlDoc.getRootElement();
        List<Element> studentList = root.elements();
        for (Element stuEl:studentList) {
            String id = stuEl.attributeValue("id");
            String name = stuEl.element("name").getText();
            char sex = stuEl.element("sex").getText().charAt(0);
            Integer age = Integer.parseInt(stuEl.element("age").getText());
            student = new Student(id,name,sex,age);
            list.add(student);
        }

        // 平均性别女年龄
        int sum = 0;
        for (Student stu:list) {
            if(stu.getSex()=='女'){
                count++;
                sum+=stu.getAge();
            }
        }

        // 打印所有女生的平均年龄
        System.out.println("所有女生的平均年龄是："+sum/(float)count);

        // 排序
        Collections.sort(list);

        // 写进去
        Document newDoc = DocumentHelper.createDocument();
        Element newRoot = newDoc.addElement("students");
        for (Student s:list) {
            Element newStuEl = newRoot.addElement("student");
            newStuEl.addElement("name").setText(s.getName());
            newStuEl.addElement("sex").setText(s.getSex()+"");
            newStuEl.addElement("age").setText(s.getAge()+"");
            newStuEl.addAttribute("id",s.getId());
        }

        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("GBK");
        FileWriter fw = new FileWriter("pkg/xml/student.xml");
        XMLWriter writer = new XMLWriter(fw,format);
        writer.write(newDoc);
        writer.close();

        // 改变性别
        Element lastStuEl = (Element)newDoc.selectSingleNode("//student[last()]");
        String sex = lastStuEl.elementText("sex");
        lastStuEl.element("sex").setText(sex.equals("男")?"女":"男");
        newRoot.remove(lastStuEl);

        // 重新写入文件
        FileWriter fw2 = new FileWriter("pkg/xml/student.xml");
        XMLWriter writer1 = new XMLWriter(fw2,format);
        writer1.write(newDoc);
        writer1.close();


    }
}

class Student implements Comparable<Student>{
    private String id;
    private String name;
    private char sex;
    private Integer age;

    public Student() {
    }

    public Student(String id, String name, char sex, Integer age) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public int compareTo(Student o) {
        return age - o.getAge();
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", sex=" + sex +
                ", age=" + age +
                '}';
    }
}